/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Linq;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// Interaction logic for MessageBox.xaml
    /// </summary>
    public partial class AssignExceptionModGroup : ModalWindow
    {

        DominicaEntities mContext;
        PRODUCT_GROUP mProdGroup;

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="closedCallback"></param>
        public AssignExceptionModGroup(DominicaEntities context, PRODUCT_GROUP pg)
            : base()
        {
            PosWindow.MainPosWindow.Topmost = false;
            InitializeComponent();
            mContext = context;
            mProdGroup = pg;
            buttonClose.MouseDown += new MouseButtonEventHandler(buttonClose_MouseDown_NOTHROW);
            buttonOk.MouseDown += new MouseButtonEventHandler(buttonOk_MouseDown_NOTHROW);
            this.bAddSelected.Click += new System.Windows.RoutedEventHandler(bAddSelected_Click);
            this.bRemoveSelected.Click += new System.Windows.RoutedEventHandler(bRemoveSelected_Click);
            this.Closing += new System.ComponentModel.CancelEventHandler(MessageBox_Closing_NOTHROW);
        }

        void bRemoveSelected_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (mProdGroup != null)
                {
                    foreach (object prodMod in listviewChosenMods.SelectedItems)
                    {
                        mProdGroup.EXCEPTION_MODIFIER_GROUPS.Remove(prodMod as EXCEPTION_MODIFIER_GROUP);
                    }
                }
                InitChoices();
                mContext.SaveChanges();
            }
            catch { }
        }

        void bAddSelected_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (mProdGroup != null)
                {
                    foreach (object prodMod in listviewAvailableMods.SelectedItems)
                    {
                        mProdGroup.EXCEPTION_MODIFIER_GROUPS.Add(prodMod as EXCEPTION_MODIFIER_GROUP);
                    }
                    InitChoices();
                    mContext.SaveChanges();
                }
            }
            catch { }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool InitChoices()
        {
            bool success = false;
            try
            {
                if (mProdGroup != null)
                {
                    List<EXCEPTION_MODIFIER_GROUP> eMods = new List<EXCEPTION_MODIFIER_GROUP>();
                    var qAvailable = from EM in mContext.EXCEPTION_MODIFIER_GROUPS
                                     where EM.IsDeleted == false
                                     orderby EM.Name
                                     select EM;

                    List<EXCEPTION_MODIFIER_GROUP> usedList = null;
                    if (qAvailable.Any())
                    {
                        usedList = mProdGroup.EXCEPTION_MODIFIER_GROUPS.ToList();
                        foreach (EXCEPTION_MODIFIER_GROUP em in qAvailable)
                        {
                            if (!usedList.Contains(em))
                            {
                                eMods.Add(em);
                            }
                        }
                    }

                    listviewAvailableMods.ItemsSource = eMods;
                    listviewChosenMods.ItemsSource = usedList;
                }
                else
                {
                    listviewAvailableMods.ItemsSource = null;
                    listviewChosenMods.ItemsSource = null;
                }
                success = true;
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return success;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MessageBox_Closing_NOTHROW(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                ModalDialogIsOpen = false;
                CurrentlyOpenInstance = null;
#if !_DEBUG
                PosWindow.MainPosWindow.Topmost = true;
#endif
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SLIST_CLOSING, exc);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        void buttonOk_MouseDown_NOTHROW(object sender, MouseButtonEventArgs e)
        {
            try
            {
                e.Handled = true;
                this.Close();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SLIST_OK_MOUSEDOWN, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        void buttonClose_MouseDown_NOTHROW(object sender, MouseButtonEventArgs e)
        {
            try
            {
                e.Handled = true;
                this.Close();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SLIST_CLOSE_MOUSEDOWN, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="msg"></param>
        public void SetMessage(String msg)
        {
            textBlockMessage.Text = msg;
        }
    }
}
